﻿/*************************************
 * Creator:peiyang
 * DateTime:2021/3/18 8:16:38
 * Description:<Description>
 * CopyRight:臭皮匠工作室
 * ***********************************/

using System.Text;

namespace Cpjit.SunnyPaine.Toolkit.ActiveMQ
{
    /// <summary>
    /// ActiveMQ的BrokerUri生成工具。
    /// </summary>
    public class BrokerUriUtil
    {
        #region 私有变量
        private static readonly string brokerUriTemplate = "failover:({ipAddresses})?" +
            "nested.wireFormat.maxInactivityDuration={maxInactivityDuration}" +
            "&nested.connectionTimeout={connectionTimeout}" +
            "&maxReconnectAttempt={maxReconnectAttempt}" +
            "&timeout={timeout}" +
            "&initialReconnectDelay={initialReconnectDelay}" +
            "&startupMaxReconnectAttempt={startupMaxReconnectAttempt}" +
            "&maxReconnectDelay={maxReconnectDelay}" +
            "&jms.useAsyncSend={useAsyncSend}" +
            "&randomize={randomize}";
        #endregion


        #region 私有方法
        /// <summary>
        /// 解析IP地址。
        /// </summary>
        /// <param name="ipAddresses"></param>
        /// <returns></returns>
        private static string ParseIP(string[] ipAddresses)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string ipAddress in ipAddresses)
            {
                if (string.IsNullOrEmpty(ipAddress))
                {
                    continue;
                }
                sb.Append($"tcp://{ipAddress},");
            }
            return sb.Remove(sb.Length - 1, 1).ToString();
        }
        #endregion


        #region 公共方法
        /// <summary>
        /// 创建BrokerUri连接字符串。
        /// </summary>
        /// <param name="ipAddresses">IP地址。</param>
        /// <param name="randomize">是否优先随机连接一个IP地址。</param>
        /// <param name="useAsyncSend">是否使用异步发送消息。默认false。</param>
        /// <returns></returns>
        public static string CreateBrokerUri(string[] ipAddresses, bool randomize = false, bool useAsyncSend = false)
        {
            return brokerUriTemplate.Replace("{ipAddresses}", ParseIP(ipAddresses))
                                    .Replace("{maxInactivityDuration}", "3000")
                                    .Replace("{connectionTimeout}", "2000")
                                    .Replace("{maxReconnectAttempt}", "2")
                                    .Replace("{timeout}", "2000")
                                    .Replace("{initialReconnectDelay}", "50")
                                    .Replace("{startupMaxReconnectAttempt}", "2")
                                    .Replace("{maxReconnectDelay}", "100")
                                    .Replace("{useAsyncSend}", useAsyncSend.ToString().ToLower())
                                    .Replace("{randomize}", randomize.ToString().ToLower());
        }

        /// <summary>
        /// 创建BrokerUri连接字符串。
        /// </summary>
        /// <param name="ipAddresses">IP地址。</param>
        /// <param name="connectionTimeout">连接超时时长。</param>
        /// <param name="randomize">是否优先随机连接一个IP地址。</param>
        /// /// <param name="useAsyncSend">是否使用异步发送消息。默认false。</param>
        /// <returns></returns>
        public static string CreateBrokerUri(string[] ipAddresses, long connectionTimeout,
            bool randomize = false, bool useAsyncSend = false)
        {
            return brokerUriTemplate.Replace("{ipAddresses}", ParseIP(ipAddresses))
                                    .Replace("{maxInactivityDuration}", "3000")
                                    .Replace("{connectionTimeout}", connectionTimeout.ToString())
                                    .Replace("{maxReconnectAttempt}", "2")
                                    .Replace("{timeout}", "2000")
                                    .Replace("{initialReconnectDelay}", "50")
                                    .Replace("{startupMaxReconnectAttempt}", "2")
                                    .Replace("{maxReconnectDelay}", "100")
                                    .Replace("{useAsyncSend}", useAsyncSend.ToString().ToLower())
                                    .Replace("{randomize}", randomize.ToString().ToLower());
        }

        /// <summary>
        /// 创建BrokerUri连接字符串。
        /// </summary>
        /// <param name="ipAddresses">IP地址。</param>
        /// <param name="connectionTimeout">连接超时时长。</param>
        /// <param name="connectAttempt">（重）连接尝试的次数。</param>
        /// <param name="randomize">是否优先随机连接一个IP地址。</param>
        /// 
        /// /// /// <param name="useAsyncSend">是否使用异步发送消息。默认false。</param>
        /// <returns></returns>
        public static string CreateBrokerUri(string[] ipAddresses, long connectionTimeout,
            int connectAttempt, bool randomize = false, bool useAsyncSend = false)
        {
            return brokerUriTemplate.Replace("{ipAddresses}", ParseIP(ipAddresses))
                                    .Replace("{maxInactivityDuration}", "3000")
                                    .Replace("{connectionTimeout}", connectionTimeout.ToString())
                                    .Replace("{maxReconnectAttempt}", connectAttempt.ToString())
                                    .Replace("{timeout}", "2000")
                                    .Replace("{initialReconnectDelay}", "50")
                                    .Replace("{startupMaxReconnectAttempt}", connectAttempt.ToString())
                                    .Replace("{maxReconnectDelay}", "100")
                                    .Replace("{useAsyncSend}", useAsyncSend.ToString().ToLower())
                                    .Replace("{randomize}", randomize.ToString().ToLower());
        }
        #endregion
    }
}
